package org.jrfe.net;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 
 * @author Kalvis Freimanis
 *
 * @param <T> Queue Type
 */
public class SyncQueue<T> {

	private Queue<T> q;
	private Object syncQ = new Object();
	public SyncQueue() {
		q = new LinkedList<T>();
	}
	
	public T readQueue(){
		T ob = null;
		synchronized (syncQ) {
			ob = q.poll();
		}
		return ob;
	}
	
	public void writeQueue(T ob){
		synchronized (syncQ) {
			q.add(ob);
		}
	}
	
	public void clearQueue(){
		synchronized (syncQ) {
			q.clear();
		}
	}
	
	/*public static void main(String[] args) {
		final NetQueue<NetPackege> q = new NetQueue<NetPackege>();
		
		Thread t1 = new Thread(new Runnable(){
			@Override
			public void run() {
				while(true){
					q.writeQueue(new NetPackege(1,"Dummy Pakage"));
					try {
						Thread.sleep(1);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});
		
		Thread t2 = new Thread(new Runnable(){
			@Override
			public void run() {
				while(true){
					NetPackege p = q.readQueue();
					if(p != null){
						if(p.getType() != 1){
							System.out.println(p.getContent().toString());
						}
					}
					try {
						Thread.sleep(1);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});
		
		t1.start();
		t2.start();
		
		String s = "";
		
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
		
		while(true){
	
			try {
				s = in.readLine();
				q.writeQueue(new NetPackege(2,s));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
		}	
	}*/
}
